package com.webcamtracker.ui.motion;

import com.webcamtracker.configuration.RootPref;
import com.webcamtracker.ui.UIDrawableCanvas;
import com.webcamtracker.ui.UITracker;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;


public class UIMotionPropertiesPanel extends JPanel {

    private UIDrawableCanvas canvas = new UIDrawableCanvas(new Dimension(320, 240));
    private UITracker uiTracker;
    private BufferedImage backgroundImage;


    public UIMotionPropertiesPanel(UITracker tracker) {
        super(true);
        this.uiTracker = tracker;
        setupUI();

    }

    private void setupUI() {

        JPanel slider = setupSlider();
        this.setLayout(new GridLayout(1, 2));
        this.add(setUpImageBackground());
        this.add(slider);
    }

    private JPanel setUpImageBackground() {
        JPanel result = new JPanel();
        CompoundBorder border = BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Image Background"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
        result.setBorder(border);
        result.add(setupButtom(), BorderLayout.WEST);
        result.add(canvas, BorderLayout.CENTER);
        return result;
    }

    private JPanel setupButtom() {
        JPanel result = new JPanel(new FlowLayout());

        JButton setBackground = new JButton("Pick");
        setBackground.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                pickBackGround();
            }
        });
        result.add(setBackground);
        return result;
    }

    protected JPanel setupSlider() {
        JPanel result = new JPanel();
        CompoundBorder border = BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Filter Properties"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5));
        result.setBorder(border);
        result.setLayout(new BorderLayout());

        JSlider euclideanDistance = new JSlider(JSlider.HORIZONTAL, 1, 20, 2);
        final JLabel distance = new JLabel("Treshold" + euclideanDistance.getValue());

        euclideanDistance.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                RootPref.getMotionPref().putInt(RootPref.KEY_MOTION_TRESHOLD, slider.getValue());
                distance.setText("Threshold" + slider.getValue());
            }
        });
        result.add(distance, BorderLayout.WEST);
        result.add(euclideanDistance, BorderLayout.CENTER);

        return result;
    }

    public void pickBackGround() {
        backgroundImage = uiTracker.getVideo().getVideoManager().getImage();
        setImage(backgroundImage);
    }


    public void setImage(final BufferedImage image) {
        canvas.add(new UIDrawableCanvas.Drawable() {
            public void paint(Graphics g) {
                g.drawImage(image, 0, 0, null);
            }
        });
        canvas.repaint();
    }


}
